package SignAndRegister;

import AccountWindow.Customer.CustomerWindow;
import AccountWindow.Employee.EmployeeWindow;
import AccountWindow.HotelAdmin.HotelAdminWindow;
import AccountWindow.SystemAdmin.SystemAdminWindow;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;


public class SignWindowHandler implements ActionListener {

    private SignWindow signWindow;
    public SignWindowHandler(SignWindow signWindow){this.signWindow=signWindow;}

    boolean loginSuccess = false;

    @Override
    public void actionPerformed(ActionEvent e) {
        String actionCommand = e.getActionCommand();
        System.out.println("actionCommand = "+actionCommand);
        if("sign".equals(actionCommand)){
            processSign();
        }else if("register".equals(actionCommand)){
            processRegister();
        }
        if(loginSuccess) signWindow.dispose();
    }

    private void processSign(){
        //记录用户输入的ID和密码值，在数据库中作查找操作，数据库返回相应值，若匹配成功，弹出相应的用户类型的界面
        String accountID = signWindow.getIDText();
        String accountCode = signWindow.getCodeText();
        String accountType = signWindow.getTypeChoice();

        try {
            loginSuccess = login(accountID,accountCode,accountType);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if("住客".equals(accountType)){   //对住客ID进行转换
            try{
                Class.forName("com.mysql.cj.jdbc.Driver"); //注册驱动
                Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hims", "root", "511300");  //获取连接
                Statement stat = con.createStatement(); //获取执行者对象
                String sql = "SELECT customerID FROM customer WHERE customerPhone=" + accountID;
                ResultSet rs = stat.executeQuery(sql);

                String targetAccountID = null;
                while(rs.next()) {
                    Integer accountIDTmp = rs.getInt("customerID");
                    targetAccountID = accountIDTmp.toString();
                }

                accountID = targetAccountID;
            }catch (Exception exception){
                exception.printStackTrace();
            }

        }

        if(loginSuccess){   //各窗口构造方法有String accountID 便于后继窗口内的操作
            if("住客".equals(accountType)){
                System.out.println("住客成功登录");
                new CustomerWindow(accountID);
            }else if("职工".equals(accountType)) {
                new EmployeeWindow(accountID);
            }else if("酒店管理员".equals(accountType)) {
                new HotelAdminWindow(accountID);
            }else if("系统管理员".equals(accountType)){
                new SystemAdminWindow(accountID);
            }
        }

    }

    private  void processRegister(){
        //弹出注册页面
        new RegisterWindow();
    }

    private static boolean login(String accountID, String accountCode,String accountType) throws Exception {
        if("住客".equals(accountType)){
            Class.forName("com.mysql.cj.jdbc.Driver"); //注册驱动
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hims", "root", "511300");  //获取连接
            Statement stat = con.createStatement(); //获取执行者对象
            String sql = "SELECT customerID FROM customer WHERE customerPhone=" + accountID;
            ResultSet rs = stat.executeQuery(sql);

            String targetAccountID = null;
            String targetAccountCode = null;
            String targetAccountType = null;

            while(rs.next()) {
                Integer accountIDTmp = rs.getInt("customerID");
                targetAccountID = accountIDTmp.toString();
            }

            if (targetAccountID != null){
                sql = "SELECT * FROM account WHERE accountID=" + targetAccountID;
                rs = stat.executeQuery(sql);
                while(rs.next()) {
                    Integer accountIDTmp = rs.getInt("accountID");
                    targetAccountID = accountIDTmp.toString();
                    targetAccountCode = rs.getString("accountCode");
                    targetAccountType = rs.getString("accountType");
                }
                if(accountCode.equals(targetAccountCode)){
                    if(accountType.equals(targetAccountType)){
                        rs.close();
                        stat.close();
                        con.close();
                        return true;
                    } else{
                        new RemindWindow("Type");
                    }
                }else{
                    new RemindWindow("Code");
                }
            }else{
                System.out.println("这里出问题");
                new RemindWindow("ID");
            }

            rs.close();
            stat.close();
            con.close();

            return false;

        }else {
            Class.forName("com.mysql.cj.jdbc.Driver"); //注册驱动
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/hims", "root", "511300");  //获取连接
            Statement stat = con.createStatement(); //获取执行者对象
            String sql = "SELECT * FROM account WHERE accountID=" + accountID;
            ResultSet rs = stat.executeQuery(sql);

            String targetAccountID = null;
            String targetAccountCode = null;
            String targetAccountType = null;

            while(rs.next()) {
                Integer accountIDTmp = rs.getInt("accountID");
                targetAccountID = accountIDTmp.toString();
                targetAccountCode = rs.getString("accountCode");
                targetAccountType = rs.getString("accountType");
            }

            if (targetAccountID != null){
                if(accountCode.equals(targetAccountCode)){
                    if(accountType.equals(targetAccountType)){
                        rs.close();
                        stat.close();
                        con.close();
                        return true;
                    } else{
                        new RemindWindow("Type");
                    }
                }else{
                    new RemindWindow("Code");
                }
            }else{
                new RemindWindow("ID");
            }

            rs.close();
            stat.close();
            con.close();

            return false;

        }
    }
}
